define(['app','assets/js/zTree'],function(app){return {module: function($scope, $http, eventBusService, httpService, params) {
		$scope.form = {};
		var currentNode = null;
		var menuTreeObj;
		var tId = "";

		//单击
		var zTreeOnClick = function(event, treeId, treeNode) {
			$scope.dept_name = treeNode.name;
			currentNode = treeNode;
			tId = currentNode.tId;

			$scope.selectTeacher(treeNode);
			$scope.$apply();
		};

		function addDiyDom(treeId, treeNode) {
			var spanObj = $("#" + treeNode.tId + "_span");

			//删除
			var deleteBtn = '<button class="btn-outline-danger btn-sm" id="deleteOrg_' + treeNode.tId + '" style="margin-left: 5px;">删除</button>';
			spanObj.after(deleteBtn);
			var deleteBtnObj = $("#deleteOrg_" + treeNode.tId);
			if (deleteBtnObj) {
				deleteBtnObj.bind("click", function() {
					$scope.deleteOneOrg(treeNode);
				});
			}
			// 添加子部门
			var addBtn = '<button class="btn-outline-primary btn-sm" id="addOrg_' + treeNode.tId + '" >子部门</button>';
			spanObj.after(addBtn);
			var addBtnObj = $("#addOrg_" + treeNode.tId);
			if (addBtnObj) {
				addBtnObj.bind("click", function() {
					$scope.addOrg(treeNode);
				});
			}
		}

		// zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
		var setting = {
			data: {
				simpleData: {
					enable: true
				}
			},
			check: {
				enable: false
			},
			edit: {
				enable: true
			},
			view:{
				addDiyDom: addDiyDom,
				showIcon: false,
				selectedMulti: false
			},
			callback: {
				onClick: zTreeOnClick
			}
		};
		// zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）

		//学校部门
		$scope.queryMenu = function(){
			var zNodes =  [];
			httpService.post(app.urls.organization_findOrganizationTree, {}, function(data){
				if("0000" === data.code) {
					zNodes = data.data;
					menuTreeObj = $.fn.zTree.init($("#organizationTree"), setting, zNodes);
					$scope.$apply();

					var nodes = menuTreeObj.getNodes();
					if (nodes.length > 0) {
						currentNode = nodes[0];
						menuTreeObj.selectNode(nodes[0]);

						menuTreeObj.expandAll(true);
						setting.callback.onClick(null, menuTreeObj.setting.treeId, nodes[0]);
					}

				}else{
					eventBusService.publish("modal.alert", {"content": data.msg});
				}
			});
		}
		$scope.queryMenu();

	//获取具体教师数据
	$scope.selectTeacher=function(row){
		$scope.show="true";
		$scope.form.fk_organization=$scope.fk_organization=row.id;
		$scope.row_name=row.name;
		$scope.type=row.type;
		$scope.father_id=row.father_id;

		if($scope.type == '1'){
			$("#"+$scope.controllerName + "_th").addClass("fe-folder" );
			$("#"+$scope.controllerName + "_th").removeClass("fe-user"); 
		}
		if($scope.type == '2'){
			$("#"+$scope.controllerName + "_th").addClass("fe-user" );
			$("#"+$scope.controllerName + "_th").removeClass("fe-folder"); 
		}
		if($scope.type == '3'){
			$("#"+$scope.controllerName + "_th").addClass("fe-user" );
			$("#"+$scope.controllerName + "_th").removeClass("fe-folder"); 
		}

		$scope.find();

	}
	
//	$scope.find=function(){
//		httpService.postpage(app.urls.organization_selectUserOrganization,{"id":$scope.fk_organization,"unitId":$scope.form.unitId},function(data){
//			if("0000" === data.code){
//				$scope.rows = data.data;		
//			}else{
//				alert(data.msg);
//			}
//		});
//	}
	$scope.find = function(){
		httpService.postpage(app.urls.organization_selectUserOrganization,{"id":$scope.form.fk_organization,"unitId":$scope.form.unitId},function(data){
			if("0000" === data.code){
				$scope.rows = data.data;
			}else{
				alert(data.msg);
			}
		});
	}
	//接收数据刷新
	eventBusService.subscribe('content.page.reload', function(event,data) {
		$scope.find();
	});
	eventBusService.subscribe('content.pageRole.reload', function(event,data) {
		$scope.queryMenu();
	});
	eventBusService.subscribe('content.pageGroup.reload', function(event,data) {
		$scope.queryMenu();
	});
	eventBusService.subscribe('content.name.reload', function(event,data) {
		$scope.row_name = data;
	});
	//按钮点击事件-条件查询
	$scope.select = function(){
		$scope.page.current = 1;
		$scope.find();
	}

	//按钮点击事件-添加分组
	$scope.addOne = function(){
    	eventBusService.publish("modal.page","modules/content/usermanagement/organization/addForg");
	}

	//按钮点击事件-添加子部门
	$scope.addOrg = function(type){
		if(type.type == '1'){
			eventBusService.publish("modal.page","modules/content/usermanagement/organization/addOrg?id="+type.id+"&type=2");
		}
		if(type.type == '2'){
			eventBusService.publish("modal.page","modules/content/usermanagement/organization/addOrg?id="+type.id+"&type=3");
		}
	}

	//按钮点击事件-添加教师
	$scope.addTeacher = function(fk_organization,type){
    	eventBusService.publish("modal.page","modules/content/usermanagement/organization/addTeacher?fk_organization="+fk_organization+"&type="+type);
	}


	//修改部门
	$scope.updateRule= function(fk_organization,father_id,type) {
		if(type == '1'){
			eventBusService.publish("modal.page","modules/content/usermanagement/organization/updateFOrg?fk_organization="+fk_organization+"&type="+type);
		}else{
			eventBusService.publish("modal.page","modules/content/usermanagement/organization/updateOrg?fk_organization="+fk_organization+"&father_id="+father_id+"&type="+type);
		}

	}

	//修改老师部门
	$scope.updateTeacher = function(){
		var values = [];
    	$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
    		values.push($(this).val());
    	});
    	if(values.length < 1){
    		eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择修改数据。"});
    	}else if(values.length > 1){
    		eventBusService.publish('modal.alert', {"title":"操作提示","content":"您选择了多行数据，请重新选择。"});
    	}else{
    		eventBusService.publish("modal.page","modules/content/usermanagement/organization/updateTeacher?id="+values[0]+"&type="+$scope.type);
    	}
	}


	//按钮点击事件-删除教师
	$scope.deleteTeacher = function(){
		var values = [];
		var ids="";
    	$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
    		values.push($(this).val());
    		ids = ids+",'"+$(this).val()+"'";
    	});

    	if(values.length < 1){
    		eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择您要删除数据。"});
    	}else{
    		eventBusService.publish("modal.alert",{"content":"你是否确认删除这"+values.length+"条数据？","isAutoClose":false,"confirm":function(){
					httpService.post(app.urls.organization_deleteUserOrganization,{"ids":ids.replace(',','')},function(data){
    				if("0000" === data.code){
						$scope.find();
    	    		}else{
    	    			alert(data.msg);
    	    		}
    			});

    		},"cancel":function(){
    			//用户取消，不做操作
    		}});

    	}

	}


	//按钮点击事件-删除一级菜单
	$scope.deleteOneOrg = function(row){
    		eventBusService.publish("modal.alert",{"content":"你是否确认删除？","isAutoClose":false,"confirm":function(){
    			httpService.post(app.urls.organization_selectTypeTwo,{"father_id":row.id},function(data){
    				if("0000" === data.code){
    					if(data.data.length == 0){
    						httpService.postpage(app.urls.organization_selectUserOrganization,{"id":row.id,"unitId":$scope.form.unitId},function(data){
    		    				if("0000" === data.code){
    		    					if(data.data.length == 0){
    		    						httpService.post(app.urls.organization_delete,{"id":row.id,"unitId":$scope.form.unitId},function(data){
    		    							if("0000" === data.code){
												$scope.selectOne();
												$scope.find();
    		    							}else{
    		    								alert(data.msg);
    		    							}
    		    						});
    		    					}else{
    		    						alert("该部门下有老师！");
    		    					}
    		    				}else{
    		    					alert(data.msg);
    		    				}
    		    			});
    					}else{
    						alert("该部门下有子部门！");
    					}
    				}else{
    					alert(data.msg);
    				}
    			});
    		},"cancel":function(){
    			//用户取消，不做操作
    		}});

	}


	//按钮点击事件-删除二级菜单
	$scope.deleteTwoOrg = function(row){
    		eventBusService.publish("modal.alert",{"content":"你是否确认删除？","isAutoClose":false,"confirm":function(){
    			httpService.post(app.urls.organization_selectTypeThree,{"father_id":row.id},function(data){
    				if("0000" === data.code){
    					if(data.data.length == 0){
    						httpService.postpage(app.urls.organization_selectUserOrganization,{"id":row.id,"unitId":$scope.form.unitId},function(data){
    		    				if("0000" === data.code){
    		    					if(data.data.length == 0){
    		    						httpService.post(app.urls.organization_delete,{"id":row.id,"unitId":$scope.form.unitId},function(data){
    		    							if("0000" === data.code){
												$scope.selectTwo();
												$scope.find();
    		    							}else{
    		    								alert(data.msg);
    		    							}
    		    						});
    		    					}else{
    		    						alert("该部门下有老师！");
    		    					}
    		    				}else{
    		    					alert(data.msg);
    		    				}
    		    			});
    					}else{
    						alert("该部门下有子部门！");
    					}
    				}else{
    					alert(data.msg);
    				}
    			});
    		},"cancel":function(){
    			//用户取消，不做操作
    		}});

	}

	//按钮点击事件-删除三级菜单
	$scope.deleteThreeOrg = function(row){
    		eventBusService.publish("modal.alert",{"content":"你是否确认删除？","isAutoClose":false,"confirm":function(){
    			httpService.postpage(app.urls.organization_selectUserOrganization,{"id":row.id,"unitId":$scope.form.unitId},function(data){
    				if("0000" === data.code){
    					if(data.data.length == 0){
    						httpService.post(app.urls.organization_delete,{"id":row.id,"unitId":$scope.form.unitId},function(data){
    							if("0000" === data.code){
									$scope.selectThree();
									$scope.find();
    							}else{
    								alert(data.msg);
    							}
    						});
    					}else{
    						alert("该部门下有老师！");
    					}
    				}else{
    					alert(data.msg);
    				}
    			});
    			
    		},"cancel":function(){
    			//用户取消，不做操作
    		}});

	}

	$scope.setHandler = function (){
		$scope.updateDept('1');
	}

	$scope.setAuditor = function (){
		$scope.updateDept('2');
	}

	$scope.updateDept = function (val){
		let values = [];
		let ids="";
		$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
			values.push($(this).val());
			ids = ids+",'"+$(this).val()+"'";
		});
		if(values.length < 1){
			eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择修改数据。"});
		}else{
			eventBusService.publish("modal.page","modules/content/usermanagement/organization/deptHandler?ids="+ids.replace(',','')+"&t="+val+"&v="+Math.random());
		}
	}

		//选择数据
		$scope.optData = function (element) {
			if($(element).find("input[name='dataPk']").is(":checked")){
				$(element).find("input[name='dataPk']").prop("checked",false);
				$(element).removeClass("opt_tr");
			}else{
				$(element).find("input[name='dataPk']").prop("checked",true);
				$(element).addClass("opt_tr");
			}
		}
		$scope.checkAlls = function(target) {
			$("#" + $scope.controllerName + " input[name='dataPk']").each(function() {
				if ($(this).prop("checked")) {
					$(this).prop('checked', false);
					$(this).parents("tr").removeClass("opt_tr");
				} else {
					$(this).prop('checked', true);
					$(this).parents("tr").addClass("opt_tr");
				}
			})
		}
}}});